
# -*- coding:UTF-8 -*-
import os, sys
import glob
from PIL import Image
import cv2
import shutil
from PIL import Image
def is_valid(file):
    valid = True
    try:
        Image.open(file).load()
    except OSError:
        valid = False
    return valid

#图像存储位置
root_path ="/home/xys/CloundShiProjects/motorbicyle_detect/motor_obj_det_data/test/0112err/0112_part"
src_img_dir = root_path + "/jpg"
src_txt_dir = root_path + "/txt"
src_xml_dir = root_path+"/xml"
# dict = {'3' : "motorback", '4' : "toukui", '8': "e_vehicle_license_vehicle_motor"}

dict = {'0': "plate",#字典对类型进行转换
                '1': "chezuo",
                '2': "mtc_dadeng",
                '3': "mtc_weideng",
                '4': "toukui",
                '5': "paiqiguan"}
#                 # '6': "xingshizheng_fuye",
#                 # # '7': "huang-zuo",
#                 # '8': "luntaiguige",
#                 # '9': "chejiahao_xing",
#                 # '11': "xingshizheng_vehicle",
#                 # '12': "xingshizheng_fanmian",
#                 # '13': "e_vehicleTripod",
#                 # '22':"erweima"
#                 }

# img = cv2.imread("/home/pc/code/jianyan_code/0611_weights/test_image/1267589092.jpg")
# cv2.imshow("img",img)
# cv2.waitKey()
if not os.path.exists(src_xml_dir):
    os.mkdir(src_xml_dir)
    print(src_xml_dir + '----创建成功')

img_Lists = glob.glob(src_img_dir + '/*.jpg')

img_basenames = []
for item in img_Lists:
    img_basenames.append(os.path.basename(item))

img_names = []
for item in img_basenames:
    temp1, temp2 = os.path.splitext(item)
    img_names.append(temp1)




for img in img_names:

    try:
        im = Image.open((src_img_dir + '/' + img + '.jpg'))
    except :
        os.remove(src_img_dir + '/' + img + '.jpg')
        os.remove(src_txt_dir + '/' + img )
        continue
    width, height = im.size
    print(img)

    # open the crospronding txt file
    gt = open(src_txt_dir + '/' + img).read().splitlines()
    # gt = open(src_txt_dir + '/gt_' + img + '.txt').read().splitlines()

    # write in xml file
    # os.mknod(src_xml_dir + '/' + img + '.xml')
    xml_file = open((src_xml_dir + '/' + img + '.xml'), 'w')
    xml_file.write('<annotation>\n')
    xml_file.write('    <folder>VOC2007</folder>\n')
    xml_file.write('    <filename>' + str(img) + '.png' + '</filename>\n')
    xml_file.write('    <size>\n')
    xml_file.write('        <width>' + str(width) + '</width>\n')
    xml_file.write('        <height>' + str(height) + '</height>\n')
    xml_file.write('        <depth>3</depth>\n')
    xml_file.write('    </size>\n')

    # write the region of image on xml file
    for img_each_label in gt:
        spt = img_each_label.split(' ')
        label = dict[spt[0]]
        a = int((float(spt[1]) - float(spt[3])/2)*width)
        b = int((float(spt[2]) - float(spt[4])/2)*height)
        c = int((float(spt[1]) + float(spt[3])/2)*width)
        d = int((float(spt[2]) + float(spt[4])/2)*height)
        xml_file.write('    <object>\n')
        xml_file.write('        <name>' + str(label) + '</name>\n')
        xml_file.write('        <pose>Unspecified</pose>\n')
        xml_file.write('        <truncated>0</truncated>\n')
        xml_file.write('        <difficult>0</difficult>\n')
        xml_file.write('        <bndbox>\n')
        xml_file.write('            <xmin>' + str(a) + '</xmin>\n')
        xml_file.write('            <ymin>' + str(b) + '</ymin>\n')
        xml_file.write('            <xmax>' + str(c) + '</xmax>\n')
        xml_file.write('            <ymax>' + str(d) + '</ymax>\n')
        xml_file.write('        </bndbox>\n')
        xml_file.write('    </object>\n')

    xml_file.write('</annotation>')

